//
//  GRDownloadCell.m
//  GRFMDownloader_Example
//
//  Created by Guo Rui on 2018/1/13.
//  Copyright © 2018年 GR. All rights reserved.
//

#import "GRDownloadCell.h"
#import "GRFMDownloadManager.h"
@interface GRDownloadCell()
@property (weak, nonatomic) IBOutlet UILabel *fileName;
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;
@property (weak, nonatomic) IBOutlet UIProgressView *progress;
@property (weak, nonatomic) IBOutlet UIButton *controlBtn;

@property (strong, nonatomic) NSURL *url;
@end

@implementation GRDownloadCell

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)updateWithFileName:(NSString *)fileName
{
    self.fileName.text = fileName;
}

- (IBAction)controlDidClick:(id)sender {
    
    GRFMDownloaderState currentState = [GRFMDownloadManager.sharedInstance downloadStateWithURL:self.url];
    if (currentState == GRFMDownloaderStatePause) {
        [GRFMDownloadManager.sharedInstance resumeWithURL:self.url];
    }
    else if (currentState == GRFMDownloaderStateDownloading) {
        [GRFMDownloadManager.sharedInstance pauseWithURL:self.url];
    }
}

- (void)downloadWithURL:(NSURL *)url
{
    self.url = url;
    
    __weak typeof(self) _self = self;
    [GRFMDownloadManager.sharedInstance download:url info:^(long long totalSize) {
        dispatch_async(dispatch_get_main_queue(), ^{
            _self.progressLabel.text = @"等待下载";
        });
    } progressChange:^(NSURL *url, float progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"%@ - %@", [[url.absoluteString componentsSeparatedByString:@"/"] lastObject], [[_self.url.absoluteString componentsSeparatedByString:@"/"] lastObject]);
            if ([_self.url isEqual:url]) {
                _self.progress.progress = progress;
                _self.progressLabel.text = [NSString stringWithFormat:@"%.0lf%%", progress * 100];
            }
        });
        
    } stateChange:^(GRFMDownloaderState state) {
        
        [_self updateState:state];
        
    } success:^(NSURL *url, NSString *cachePath) {
        
    } faile:^{
        
    }];
    
    [self updateState:[GRFMDownloadManager.sharedInstance downloadStateWithURL:self.url]];
}


- (void)updateState:(GRFMDownloaderState)state
{
    switch (state) {
        case GRFMDownloaderStateUnKnow:
        {
            // TODO: 下载状态 分为 未开始，下载，暂停，失败重新开始，下载完成
            self.controlBtn.enabled = NO;
            self.progress.progress = 0;
            self.progressLabel.text = @"等待中";
        }
            break;
        case GRFMDownloaderStateDownloading:
        {
            [self.controlBtn setTitle:@"正在下载" forState:UIControlStateNormal];
        }
            break;
        case GRFMDownloaderStatePause:
        {
            [self.controlBtn setTitle:@"已暂停" forState:UIControlStateNormal];
            self.progress.progress = [GRFMDownloadManager.sharedInstance downloadProgressWithURL:self.url];
            self.progressLabel.text = [NSString stringWithFormat:@"%.0lf%%", self.progress.progress  * 100];
        }
            break;
        case GRFMDownloaderStateDownloadFailed:
        {
            [self.controlBtn setTitle:@"请重试" forState:UIControlStateNormal];
            self.progress.progress = 0;
            self.progressLabel.text = @"下载失败";
        }
            break;
        case GRFMDownloaderStateDownloadSuccess:
        {
            [self.controlBtn setTitle:@"下载成功" forState:UIControlStateNormal];
            self.progress.progress = 1;
            self.progressLabel.text = @"100%";
        }
            break;
        default:
            break;
    }
}



@end
